import { Component } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Subscription, ReplaySubject } from 'rxjs';
import { CompanyService } from '../company.service';
import { CompanyGovernanceService } from './companyGovernance.service';
import { RelativePerformanceFullModel } from './components/companyGovernanceSummary/models/relativePerformanceFull.model';
import { IRelativePerformance } from './components/companyGovernanceSummary/models/IRelativePerformance.interface';
import { CompanyReportDownloadModel } from '../shared/companyHeader/companyReportDownload/companyReportDownload.model';
import { LongRunningTaskTypeEnum } from '../../../../framework/lrtm/lrtm.service';
import { CompanyModel } from '../company.model';
import { ReportAnalytics } from '../../../../shared/models/googleAnalytics.model';
import { GovernanceInsightModel } from '../governance/models/companyGovernanceInsight.model'
/**
 * Company governance main component
 *
 * Author: Sorin Spiru
 * Date: 13.01.2018
 */
@Component({
    moduleId: module.id,
    templateUrl: 'companyGovernance.component.html',
    providers: [CompanyGovernanceService]
})
export class CompanyGovernanceComponent {
    private sub: Subscription;
    private menuSub: Subscription;
    private relativeSubject: ReplaySubject<IRelativePerformance>;
    private menuItems: any[] = [];
    public company: CompanyModel;
    public downloadData: CompanyReportDownloadModel;
    public insights: GovernanceInsightModel[];
    public analyticsData: ReportAnalytics = new ReportAnalytics('Research', 'PDFCorporateGovernanceReport');

    constructor(
        private route: ActivatedRoute,
        public companyService: CompanyService,
        public companyGovernanceService: CompanyGovernanceService) {
    }

    ngOnInit() {
        this.companyService.menuItems.next(this.route.routeConfig.children);

        this.sub = this.companyService.company.subscribe(data => {
            this.company = data;           

            // set download report data
            this.downloadData = this.companyService.getGovernanceDownloadData(this.company);

            this.companyGovernanceService.governanceProfile.subscribe(profile => {
                if (profile.insights != undefined && profile.insights.length > 0)
                    this.insights = profile.insights;
            });

            this.companyGovernanceService.getCompanyGovernanceProfile(data.id);
            this.companyGovernanceService.getGovernanceOverallPerformance(data.id);
            this.relativeSubject = this.companyGovernanceService.getGovernanceDetailedRelativePerformance(data.id);
        });

        this.menuSub = this.companyGovernanceService.relativePerformance.subscribe(data => {
            let relativeData: RelativePerformanceFullModel = data as RelativePerformanceFullModel
            relativeData.issues.forEach(issue => {
                this.menuItems.push({
                    title: issue.name,
                    path: ['governance', 'issue', issue.number]
                });
            });

            //update routes data
            let issueRoute = this.route.routeConfig.children.find(x => x.path == 'issue/:number');
            issueRoute.data.menuItems = this.menuItems;

            //notify the companyMenu component to display sub menu items
            this.companyService.menuItems.next(this.route.routeConfig.children);
            issueRoute.data.menuItems = [];
        });
    }

    ngOnDestroy() {
        //this.sub.unsubscribe();
        this.menuSub.unsubscribe();
        //this.relativeSubject.complete();
    }
}